<?php

/*
  This model provides all interfaces for jobs management
 */

class Jobs_model extends CI_Model {

    protected $lang_id;

    public function __construct() {
        parent::__construct();
        $this->lang_id = get_language_id();
    }

    public function getJobById($id) {
        $this->db->select("*");
        $this->db->from("jobs");
        $this->db->join("jobs_description", 'jobs.job_id = jobs_description.job_id');
        $this->db->where("jobs.job_id", $id);
        $this->db->where("jobs_description.language_id", $this->lang_id);
        $result = $this->db->get()->result();
        return !empty($result[0]) ? $result[0] : array();
    }

    public function getJobDescriptions($id) {
        $this->db->select("*");
        $this->db->from("jobs_description");
        $this->db->where("job_id", $id);
        return $this->db->get()->result();
    }

    public function getJobs($data = array(), $limit = null, $start = null, $sort = array()) {
        $this->db->select("*");
        $this->db->from("jobs");
        $this->db->join("jobs_description", 'jobs.job_id = jobs_description.job_id');
        $this->db->where("language_id", $this->lang_id);
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                if ($key == 'search') {
                    $where = "( jobs_description.name LIKE '%" . $value . "%')";
                    $this->db->where($where);
                }
                else
                    $this->db->where($key, $value);
            }
        }
        if ($limit)
            $this->db->limit($limit, $start);

        if (!empty($sort)) {
            foreach ($sort as $key => $value) {
                $this->db->order_by($key, $value);
            }
        } else {
            $this->db->order_by('jobs.sort_order', 'ASC');
        }
        $query = $this->db->get();

        return $query->result();
    }

    public function totalJobs($data = array()) {
        $this->db->select("*");
        $this->db->from("jobs");
        $this->db->join("jobs_description", 'jobs.job_id = jobs_description.job_id');
        $this->db->where("language_id", $this->lang_id);
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                if ($key == 'search') {
                    $where = "( jobs_description.name LIKE '%" . $value . "%')";
                    $this->db->where($where);
                }
                else
                    $this->db->where($key, $value);
            }
        }
        $query = $this->db->get();

        return $query->num_rows();
    }

    public function insertJob($data) {
        $dataJobs['sort_order'] = !empty($data['sort_order']) ? $data['sort_order'] : 0;
        $job_id = $this->insert($dataJobs);
        foreach ($data['languages'] as $key => $language) {
            $dataLanguage['language_id'] = $key;
            $dataLanguage['name'] = $language['name'];
            $dataLanguage['description'] = $language['description'];
            $dataLanguage['sort_description'] = $language['sort_description'];
            $dataLanguage['meta_description'] = $language['meta_description'];
            $dataLanguage['meta_keyword'] = $language['meta_keyword'];
            $dataLanguage['tag'] = $language['tag'];
            $dataLanguage['job_id'] = $job_id;
            $this->insertDescription($dataLanguage);
        }
        return $job_id;
    }

    public function updateJobs($job_id, $data) {
        $dataJobs['sort_order'] = !empty($data['sort_order']) ? $data['sort_order'] : 0;
        $this->update($job_id,$dataJobs);
        $this->deleteJobDescription($job_id);
        foreach ($data['languages'] as $key => $language) {
            $dataLanguage['language_id'] = $key;
            $dataLanguage['name'] = $language['name'];
            $dataLanguage['description'] = $language['description'];
            $dataLanguage['sort_description'] = $language['sort_description'];
            $dataLanguage['meta_description'] = $language['meta_description'];
            $dataLanguage['meta_keyword'] = $language['meta_keyword'];
            $dataLanguage['tag'] = $language['tag'];
            $dataLanguage['job_id'] = $job_id;
            $this->insertDescription($dataLanguage);
        }
    }

    public function deleteJobDescription($id) {
        $this->db->where('job_id', $id);
        $this->db->delete('jobs_description');
    }

    public function insert($data) {
        $this->db->set('date_added', 'NOW()', FALSE);
        $this->db->insert('jobs', $data);
        return $this->db->insert_id();
    }

    public function insertDescription($data) {
        $this->db->insert('jobs_description', $data);
        return $this->db->insert_id();
    }

    public function update($id, $data) {
        $this->db->where('job_id', $id);
        $this->db->update('jobs', $data);
    }

}